package com.feibi.study.Adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.feibi.study.Interface.OnItemClickListener
import com.feibi.study.R
import java.util.*

class MenuAdapter(
    var context: Context,
    var menus: ArrayList<String>,
    var onItemClickListener: OnItemClickListener
) : RecyclerView.Adapter<MenuAdapter.ViewHolder?>() {
    var menuIcons: ArrayList<Int>? = null

    @NonNull
    override fun onCreateViewHolder(@NonNull viewGroup: ViewGroup, i: Int): ViewHolder {
        val view: View = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_menu, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(@NonNull viewHolder: ViewHolder, @SuppressLint("RecyclerView") i: Int) {
        viewHolder.apply {
            iv_icon.visibility = View.GONE
            menuIcons?.let {
                if (it.size > i) {
                    iv_icon.visibility = View.VISIBLE
                    Glide.with(context).load(it[i]).into(iv_icon)
                }
            }
            tv_item.text = menus[i]
            itemView.setOnClickListener { view ->
                onItemClickListener.onClick(i, view)
            }
            v_line.visibility = if (i + 1 == itemCount) View.INVISIBLE else View.VISIBLE
        }
    }

    override fun getItemCount(): Int {
        return menus.size
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var tv_item: TextView = view.findViewById(R.id.tv_item)
        var iv_icon: ImageView = view.findViewById(R.id.iv_icon)
        val v_line: View = view.findViewById(R.id.v_line)
    }
}